package com.zzyt.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zzyt.mylibrary.Person;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {


    private String currentInput1;
    private String currentInput2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Person person = new Person();
        TextView tvContent = findViewById(R.id.tv_content);
        tvContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, HelloActivity.class);
                startActivity(intent);
            }
        });
        EditText etInput1 = findViewById(R.id.et_input1);
        EditText etInput2 = findViewById(R.id.et_input2);


        etInput1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (etInput1.hasFocus()) {

                    String num = s.toString().trim();
                    if (num.isEmpty()) {
                        num = "0";
                    }
                    if (Integer.valueOf(num) - 5 > 0) {
                        etInput1.setText(currentInput1);
                        etInput1.setSelection(currentInput1.length());

                    } else {
                        currentInput1 = s.toString();
                        etInput2.setText((5 - Integer.valueOf(num)) + "");

                    }

                }
            }
        });


        etInput2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (etInput2.hasFocus()) {
                    String num = s.toString().trim();
                    if (num.isEmpty()) {
                        num = "0";
                    }
                    if (Integer.valueOf(num) - 5 > 0) {
                        etInput2.setText(currentInput2);
                        etInput2.setSelection(currentInput2.length());
                    } else {
                        currentInput2 = s.toString();
                        etInput1.setText((5 - Integer.valueOf(num)) + "");

                    }
                }
            }
        });
    }
}